/**
 * 零部件信息发布类
 * 
 * @author 魏文杰 
 * @Version 1.0
 * @Date 2013-10-08
 */

package ext.generic.erp.service;

import java.util.Iterator;
import java.util.List;

import ext.com.core.CoreUtil;
import ext.generic.erp.common.CommonPDMUtil;
import ext.generic.erp.rule.BussinessRule;
import ext.generic.erp.task.PartIETask;
import ext.generic.erp.util.BusinessRuleXMLConfigUtil;
import ext.generic.erp.util.IntegrationConstant;
import ext.generic.erp.util.PDMIntegrationLogUtil;

import wt.fc.Persistable;
import wt.fc.collections.WTArrayList;
import wt.part.WTPart;
import wt.util.WTException;

public class PartReleaseService {
	
	private static boolean verbose = PDMIntegrationLogUtil.verbose ;
	
	private static String clazz = PartReleaseService.class.getName() ;
	
	
	/**
	 * 发布PBO物料信息
	 * 
	 * @param obj
	 */
	public static void release(Object obj){
		release( obj , "" ) ;
	}
	
	/**
	 * 发布PBO物料信息
	 * 
	 * @param obj
	 * @param releaseTime
	 */
	public static void release(Object obj , String releaseTime){
		if( obj != null && obj instanceof WTPart){
			WTPart part = ( WTPart ) obj ;
			
			release( part , releaseTime ) ;
		}
	}
	
	/**
	 * 发布零部件物料信息
	 * 
	 * @param part
	 */
	public static void release(WTPart part){
		release( part , "" ) ;
	}
	
	/**
	 * 发布零部件物料信息
	 * 
	 * @param part
	 * @param releaseTime
	 */
	public static boolean release(WTPart part , String releaseTime){
		
		boolean flag = false;
		boolean canRelease = BussinessRule.canReleasePart(part) ;
		System.out.println("release part:::" + part.getNumber() + ",canRelease:::" + canRelease);
		if( canRelease ){
			try{
				if(verbose){
					PDMIntegrationLogUtil.printLog(clazz, startReleasePartLogInfo(part)  , IntegrationConstant.LOG_LEVEL_INFO) ;
				}
				
				//获取同一视图下最新版本的零部件
				part = CoreUtil.getWTPartByNumberAndView(part.getNumber(), part.getViewName()) ;
				
				PartIETask.add( part , releaseTime ) ;
				
				if(verbose){
					PDMIntegrationLogUtil.printLog(clazz, finishReleasePartLogInfo(part)  , IntegrationConstant.LOG_LEVEL_INFO) ;
				}
				
				if( PartIETask.hasObject( part )){
					/**
					 * @author xuge_ning
					 * @reason 物料发布到中间表失败反馈信息
					 */
					flag = true;
					if(verbose){
						PDMIntegrationLogUtil.printLog(clazz, "物料发布成功，PDM系统状态设置为：" +IntegrationConstant.PDM_RELEASE_STATUS_CN  , IntegrationConstant.LOG_LEVEL_INFO) ;
					}
					
					UpdatePDMDataService.updatePDMSucessful( part , BusinessRuleXMLConfigUtil.getInstance().getPartReleaseStatus() );
					
					if(verbose){
						PDMIntegrationLogUtil.printLog(clazz, "物料发布成功，PDM系统状态设置完成!" , IntegrationConstant.LOG_LEVEL_INFO) ;
					}
				}else{
					if(verbose){
						PDMIntegrationLogUtil.printLog(clazz, finishReleasePartLogInfo(part)  , IntegrationConstant.LOG_LEVEL_INFO) ;
						
						PDMIntegrationLogUtil.printLog(clazz, "物料发布失败，中间表没有查询到上述物料信息......" , IntegrationConstant.LOG_LEVEL_ERROR) ;
					}

					UpdatePDMDataService.updatePDMFailed( part , BusinessRuleXMLConfigUtil.getInstance().getPartReleaseStatus() );
				}
			}catch(Exception e){
				PDMIntegrationLogUtil.printLog(clazz, failedReleasePartLogInfo(part) , IntegrationConstant.LOG_LEVEL_ERROR) ;
				PDMIntegrationLogUtil.printLog(clazz, e.getMessage() , IntegrationConstant.LOG_LEVEL_ERROR) ;
			}
		}else{
			PDMIntegrationLogUtil.printLog(clazz, notReleasePartLogInfo(part)  , IntegrationConstant.LOG_LEVEL_WARN) ;
		}
		return flag;
	}
	
	/**
	 * 发布零部件物料信息
	 * 
	 * @param parts
	 */
	public static void release(List<WTPart> parts){
		release( parts , "") ;
	}
	
	/**
	 * 发布零部件物料信息
	 * 
	 * @param parts
	 * @param releaseTime
	 */
	public static void release(List<WTPart> parts , String releaseTime){
		if( parts == null ){
			return ;
		}else{
			Iterator<WTPart> ite = parts.iterator() ;
			
			while( ite.hasNext() ){
				WTPart part = ite.next() ;
				
				if( part != null ){
					release( part , releaseTime ) ;
				}
			}
		}
	}
	
	/**
	 * 发布零部件物料信息
	 * 
	 * @param parts
	 */
	public static void releasePersistable( WTArrayList parts){
		releasePersistable( parts , "" ) ;
	}
	
	/**
	 * 发布零部件物料信息
	 * 
	 * @param parts
	 * @param releaseTime
	 */
	public static boolean releasePersistable( WTArrayList parts , String releaseTime ){
		boolean flag = false;
		if( parts == null ){
			return flag;
		}
		
		try {
			int size = parts.size() ;
			for(int i = 0 ; i < size ; i++ ){
				Persistable persistable = parts.getPersistable(i) ;
				
				if( persistable != null && persistable instanceof WTPart ){
					WTPart part = ( WTPart ) persistable ;
					flag = release( part , releaseTime ) ;
					if(!flag){
						break;
					}
				}
				
			}
		} catch (WTException e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	private static String startReleasePartLogInfo(WTPart part){
		String logInfo = "开始发布零部件信息到ERP：\n" ;
		logInfo = logInfo + CommonPDMUtil.getPartInfo( part );
		return logInfo ;
	}
	
	private static String finishReleasePartLogInfo(WTPart part){
		String logInfo = "成功发布零部件信息到ERP：\n" ;
		logInfo = logInfo + CommonPDMUtil.getPartInfo( part );
		return logInfo ;
	}
	
	private static String failedReleasePartLogInfo(WTPart part){
		String logInfo = "发布零部件信息到ERP失败：\n" ;
		logInfo = logInfo + CommonPDMUtil.getPartInfo( part );
		return logInfo ;
	}
	
	private static String notReleasePartLogInfo(WTPart part){
		String logInfo = "零部件信息不发布到ERP：\n" ;
		logInfo = logInfo + CommonPDMUtil.getPartInfo( part );
		return logInfo ;
	}
}
